package stream;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

public class StreamDemo5 {
    public static void main(String[] args) {
        List<Student> slist = Arrays.asList(
                new Student("zhangsan", 19, 10000, "武汉"),
                new Student("lisi", 29, 5000, "北京"),
                new Student("wangwu", 12, 9000, "南京"),
                new Student("zhaoliu", 33, 11000, "合肥"),
                new Student("tianqi", 22, 20000, "天津")
        );
        //1.求所有学生的薪资和
        Integer integer = slist.stream().map(s -> s.getSalary()).reduce((a, b) -> a + b).get();
        System.out.println("和为：" + integer);
        //方式2
        Integer integer1 = slist.stream().map(Student::getSalary).reduce(Integer::sum).get();
        System.out.println("和为：" + integer1);
        //方式3
        int sum = slist.stream().mapToInt(Student::getSalary).sum();
        System.out.println("和为：" + sum);
        //方式4
        Integer reduce1 = slist.stream().reduce(0, (a, b) -> a += b.getSalary(), (a, b) -> a + b);
        System.out.println("和为：" + reduce1);
        System.out.println("--------------------------------");

        //求所有学生的平均薪资 mapToInt作用是：将流中的元素映射成int值，average作用是：将流中的元素映射成double值，然后计算所有元素的平均值
        double v = slist.stream().mapToInt(Student::getSalary).average().orElse(0);
        System.out.println("平均薪资为：" + v);

        //求所有学生的最高薪资
        Integer integer2 = slist.stream().map(Student::getSalary).reduce((a, b) -> a > b ? a : b).get();
        System.out.println("最高薪资为：" + integer2);
        //方式2
        Integer reduce = slist.stream().reduce(0, (a, b) -> a > b.getSalary() ? a : b.getSalary(), Integer::max);
        System.out.println("最高薪资为：" + reduce);
        //求最低薪资
        Integer reduce2 = slist.stream().map(Student::getSalary).min(Integer::compareTo).get();
        System.out.println("最低薪资为：" + reduce2);
    }

    public static void main1(String[] args) {
        // 使用归约求和
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
        Integer integer = list.stream().reduce((a, b) -> a + b).get();
        System.out.println("和为：" + integer);
        //方式2
        Integer integer1 = list.stream().reduce(Integer::sum).get();
        System.out.println("和为：" + integer1);
        //方式3
        Integer integer2 = list.stream().reduce(0, Integer::sum);
        System.out.println("和为：" + integer2);

        // 求乘积
        Integer integer3 = list.stream().reduce((a, b) -> a * b).get();
        System.out.println("乘积为：" + integer3);
        //方式2
        Integer integer4 = list.stream().reduce(Integer::sum).get();
        System.out.println("乘积为：" + integer4);


    }
}

class Student {
    private String name;
    private int age;
    private int salary;
    private String address;

    public Student() {
    }

    public Student(String name, int age, int salary, String address) {
        this.name = name;
        this.age = age;
        this.salary = salary;
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && salary == student.salary && Objects.equals(name, student.name) && Objects.equals(address, student.address);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age, salary, address);
    }

    @Override
    public String toString() {
        return "姓名：" + name + "，年龄：" + age + "，薪资：" + salary + "，地址：" + address;
    }
}